#include "comm.h"

int main()
{
  key_t key = ftok(PATHNAME, PROJ_ID);
  if(key < 0)
  {
    perror("ftok faild:");
    exit(1);
  }
  // printf("key:%d,", key);
  
  // 创建共享内存,且基于共享内存的权限为0666
  int shmid = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0666);
  if(shmid < 0)
  {
    perror("shmget faild");
    exit(1);
  }
  // printf(" shmid:%d -> shmid get success\n", shmid);
  // sleep(5);

  // 关联进程和共享内存
  char* mem = (char*)shmat(shmid, NULL, 0);
  printf("attach success\n");
  // sleep(5);

  /*------通信逻辑-----*/
  while(1)
  {
    printf("client-> %s\n", mem);
    sleep(1);
    if(strlen(mem))
    {    
      printf("client-> %s\n", mem);
      break;
    }
  }

  // 取消关联进程和共享内存
  shmdt(mem);
  printf("deatach success\n");
  // sleep(5);

  // 删除共享内存
  shmctl(shmid, IPC_RMID, NULL);
  printf("key:%d, shmid:%d -> shmid delete success\n", key, shmid);
  // sleep(5);

  return 0;
}
